//
//  BGMessageListLayout.m
//  bigMall
//
//  Created by LTOVE on 16/7/25.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGMessageListLayout.h"
#import "BGMessageNameAndValueModel.h"
#import "MJExtension.h"
@implementation BGMessageListLayout


+ (UIView *)initWithFrame:(CGRect)frame andDataArray:(NSArray *)dataArray andIsDouble:(BOOL)isDouble
{
    BGMessageListLayout * view = [[BGMessageListLayout alloc]initWithFrame:frame];
    view.backgroundColor = [UIColor whiteColor];
    view.hidden = !dataArray.count;
    
    [view setDoubleLineWidth:view andDataArray:dataArray andIsDouble:isDouble];
//    if (!isDouble) {
//        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, view.height - 1, view.width, 1)];
//        label.backgroundColor = BGtextGraykColor;
//        [view addSubview:label];
//    }
    
    return view;
}

- (void)setDoubleLineWidth:(UIView *)view andDataArray:(NSArray *)dataArray andIsDouble:(BOOL)isDouble
{
    int colCount = isDouble ? 2:1;
    CGFloat maginX = BGMaginLeft;
    CGFloat maginY = BGMagListMaginY;
    CGFloat width = (view.width - (colCount ) * maginX) / (colCount);
    CGFloat heigh = BGLabelHeigh;
    for (int i = 0 ; i < dataArray.count; i ++) {
        BGMessageNameAndValueModel *model = [BGMessageNameAndValueModel objectWithKeyValues:dataArray[i]] ;
//        GWLog(@"%@",model.value);
        NSString *str = isDouble ? [NSString stringWithFormat:@"%@: %@",model.title,model.value] :[NSString stringWithFormat:@"%@: %@",model.title,model.value];
        NSDictionary *attDic = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:12],
                                 NSForegroundColorAttributeName:BGtextGraykColor,
                                 };
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:str attributes:attDic];
        NSRange rang = [str rangeOfString:@":"];
        NSUInteger location = isDouble ? 0 : 0;
        [attr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(location, rang.location + 1)];
        int row = i % colCount;
        int  col = i / colCount;
        CGFloat x = maginX + row * (maginX + width );
        CGFloat y = maginY + col * (maginY + heigh);
        UILabel *label = [[UILabel alloc]init];
        label.attributedText = attr;
        [label sizeToFit];
        label.point = CGPointMake(x, y);
        [view addSubview:label];
    }
    
}


@end
